//
//  WMEMoreViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/2.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEMoreViewController.h"
#import "WMEMoreTableViewCell.h"
#import "WMEFirstPageModel.h"
#import "WMEUserViewController.h"
#import <AFNetworking.h>
#import <Masonry.h>
#import <MJRefresh.h>

#pragma mark - 点击赞更多
@interface WMEMoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *moreTableView;
@property (nonatomic, copy)NSString *urlStr;
@property (nonatomic, retain)NSMutableArray *moreArr;
@property (nonatomic, retain)NSNumber *more;
@property (nonatomic, assign)NSInteger count;
@end


@implementation WMEMoreViewController

- (void)dealloc
{
    [_moreTableView release];
    [_urlStr release];
    [_more release];
    [_moreArr release];
    [_more release];
    [_str release];
    [_maxCount release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    self.navigationItem.title = @"更多赞";
    [self createView];
    [self data];
    
    [self loadingFunction];
}
#pragma mark - 创建视图
- (void)createView
{
    __block __typeof(&*self)ws = self;
    self.moreTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.view addSubview:_moreTableView];
    [_moreTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
    }];
    _moreTableView.delegate = self;
    _moreTableView.dataSource = self;
    _moreTableView.separatorStyle = NO;
    [_moreTableView release];
}
#pragma mark - 数据请求
- (void)data
{
    NSString *temp = @"http://www.duitang.com/napi/blog/events/?__dtac=%257B%2522_r%2522%253A%2520%2522636846%2522%257D&app_code=gandalf&app_version=5.9%20rv%3A150681&blog_id=533232636&device_name=Unknown%20iPhone&device_platform=iPhone7%2C2&limit=0&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=667&screen_width=375&start=0&types=EVENT_BLOG_LIKE";
    self.urlStr = [temp stringByReplacingOccurrencesOfString:@"blog_id=533232636" withString:[NSString stringWithFormat:@"blog_id=%@", _str]];
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:_urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //      NSLog(@"%@", responseObject);
        NSArray *arr = responseObject[@"data"][@"object_list"];
        self.more = responseObject[@"data"][@"more"];
        _count += 24;
        self.moreArr = [WMEFirstPageModel modelHandlerWithArray:arr];
        [_moreTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}
#pragma mark - tableview 的 协议方法

#pragma mark - cell 的个数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _moreArr.count;
}

#pragma mark - cell的设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_moreTableView registerClass:[WMEMoreTableViewCell class] forCellReuseIdentifier:@"moreCell"];
    WMEMoreTableViewCell *moreCell = [_moreTableView dequeueReusableCellWithIdentifier:@"moreCell" forIndexPath:indexPath];
    if (_moreArr != 0) {        
        WMEFirstPageModel *model = _moreArr[indexPath.row];
        moreCell.moreModel = model;
    }
    return moreCell;
}

#pragma mark - 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark - cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WMEFirstPageModel *model = _moreArr[indexPath.row];
    NSString *userTemp = @"http://www.duitang.com/napi/people/profile/?__dtac=%257B%2522_r%2522%253A%2520%2522998121%2522%257D&_ts=1452865118.002704&app_code=gandalf&app_version=5.8%20rv%3A149591&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=relationship%2Cbind_status%2Cinterests%2Cidentity%2Ccity%2Ccitycode%2Cgender%2Cbirthday%2Crecommend_info%2Cclub_count&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&user_id=11200118";
    NSString *userStr = [userTemp stringByReplacingOccurrencesOfString:@"11200118" withString:model.supportId.stringValue];
    //    NSLog(@"%@", userStr);
    //AFN网络请求
    AFHTTPSessionManager *userData = [AFHTTPSessionManager manager];
    [userData GET:userStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSMutableDictionary *dic = responseObject[@"data"];
        WMEUserViewController *userVC = [[WMEUserViewController alloc] init];
        UINavigationController *userNavi = [[UINavigationController alloc] initWithRootViewController:userVC];
        userVC.userId = model.supportId.stringValue;
        userVC.userDic = dic;
        [self presentViewController:userNavi animated:YES completion:^{
        }];
        [userVC release];
        [userNavi release];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}
#pragma mark - 加载
- (void)loadingFunction
{
    if (_maxCount.integerValue > 24) {
        self.moreTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        // 设置了底部inset
        self.moreTableView.contentInset = UIEdgeInsetsMake(0, 0, 30, 0);
        // 忽略掉底部inset
        self.moreTableView.mj_footer.ignoredScrollViewContentInsetBottom = 30;
    }
}
#pragma mark - 加载的方法
- (void)loadMoreData
{
    if (_maxCount.integerValue < 24) {
        NSLog(@"数据已加载完成 不需要再加载");
        [self.moreTableView.mj_footer endRefreshingWithNoMoreData];
    }else{
        NSString *newUrlStr = [[_urlStr stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _count]]stringByReplacingOccurrencesOfString:@"limit=0" withString:[NSString stringWithFormat:@"limit=%@", _maxCount]];
        AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
        [data GET:newUrlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            nil;
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //      NSLog(@"%@", responseObject);
            NSArray *arr = responseObject[@"data"][@"object_list"];
            self.more = responseObject[@"data"][@"more"];
            _count += 24;
            NSArray *tempArr = [WMEFirstPageModel modelHandlerWithArray:arr];
            [self.moreArr addObjectsFromArray:tempArr];
            [_moreTableView reloadData];
            if (_count > _maxCount.integerValue) {
                [self.moreTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.moreTableView.mj_footer endRefreshing];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            nil;
        }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
